//
//  YCTrainCarDetailViewController.m
//  YCSchool
//
//  Created by yanglinxia on 16/4/27.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  练车计划详情

#import "YCTrainCarDetailViewController.h"
#import "YCTrainMethodButton.h"
#import "YCTrainDetailModel.h"
#import "YCCarServiceViewController.h"

@interface YCTrainCarDetailViewController ()

/** scrollView */
@property (nonatomic, strong) UIScrollView *scrollView;
/** containerView */
@property (nonatomic, strong) UIView *containerView;

/** 日 */
@property (nonatomic, strong) UILabel *daytLabel;
/** 月 */
@property (nonatomic, strong) UILabel *monthLabel;

/** 训练项目 */
@property (nonatomic, strong) UILabel *projectLabel;
/** 训练项目内容 */
@property (nonatomic, strong) UILabel *projectContentLabel;

/** 到场时间 */
@property (nonatomic, strong) UILabel *comeTimeLabel;
/** 到场时间内容 */
@property (nonatomic, strong) UILabel *comeTimeContentLabel;

/** 训练模式 */
@property (nonatomic, strong) UILabel *methodLabel;
/** 训练模式内容 */
@property (nonatomic, strong) UILabel *methodContentLabel;

/** 训练场地 */
@property (nonatomic, strong) UILabel *placeLabel;
/** 训练场地内容 */
@property (nonatomic, strong) UILabel *placeContentLabel;

/** 指导教练 */
@property (nonatomic, strong) UILabel *coachLabel;
/** 指导教练名字 */
@property (nonatomic, strong) UILabel *coachContentLabel;

/** 前往班车 */
@property (nonatomic, strong) YCTrainMethodButton *comeButton;
/** 联系教练 */
@property (nonatomic, strong) YCTrainMethodButton *contactCoachButton;
/** 已完成 */
@property (nonatomic, strong) UIImageView *completeImageView;

/** 上一个传过来的模型  */
@property (nonatomic, strong) YCTrainPlanModel *trainModel;

/** 新请求的数据  */
@property (nonatomic, strong) YCTrainDetailModel *model;
/** 请求数据中的数组  */
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation YCTrainCarDetailViewController

- (instancetype)initWithModel:(YCTrainPlanModel *)model
{
    if (self = [super init]) {
        self.trainModel = model;
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self yc_addLeftBarButtonItemWithTitle:@"关闭" target:self.navigationController action:@selector(popViewControllerAnimated:)];
    
    [self makeView];
    [self loadData];
    self.scrollView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
}

- (void)makeView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:self.scrollView];
    
    UIView *containerView = [[UIView alloc] init];
    self.containerView = containerView;
    [self.scrollView addSubview:containerView];
    
    self.daytLabel = [[UILabel alloc] init];
    self.daytLabel.font = [UIFont systemFontOfSize:20];
    self.daytLabel.text = @"15";
    self.daytLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.daytLabel];
    
    self.monthLabel = [[UILabel alloc] init];
    self.monthLabel.font = [UIFont systemFontOfSize:8];
    self.monthLabel.text = @"四月";
    self.monthLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.monthLabel];

    self.projectLabel = [[UILabel alloc] init];
    self.projectLabel.font = [UIFont systemFontOfSize:17];
    self.projectLabel.text = @"训练项目";
    self.projectLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.projectLabel];
    
    self.projectContentLabel = [[UILabel alloc] init];
    self.projectContentLabel.font = [UIFont systemFontOfSize:14];
    self.projectContentLabel.text = @"场地训练 半坡起步";
    self.projectContentLabel.textColor = [UIColor lightGrayColor];
    [containerView addSubview:self.projectContentLabel];
    
    self.comeTimeLabel = [[UILabel alloc] init];
    self.comeTimeLabel.font = [UIFont systemFontOfSize:17];
    self.comeTimeLabel.text = @"到场时间";
    self.comeTimeLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.comeTimeLabel];
    
    self.comeTimeContentLabel = [[UILabel alloc] init];
    self.comeTimeContentLabel.font = [UIFont systemFontOfSize:14];
    self.comeTimeContentLabel.text = @"2016年4月25日 上午9:00";
    self.comeTimeContentLabel.textColor = [UIColor lightGrayColor];
    [containerView addSubview:self.comeTimeContentLabel];
    
    self.methodLabel = [[UILabel alloc] init];
    self.methodLabel.font = [UIFont systemFontOfSize:17];
    self.methodLabel.text = @"训练模式";
    self.methodLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.methodLabel];
    
    self.methodContentLabel = [[UILabel alloc] init];
    self.methodContentLabel.font = [UIFont systemFontOfSize:14];
    self.methodContentLabel.text = @"5人组集中训练";
    self.methodContentLabel.textColor = [UIColor lightGrayColor];
    [containerView addSubview:self.methodContentLabel];
    
    self.placeLabel = [[UILabel alloc] init];
    self.placeLabel.font = [UIFont systemFontOfSize:17];
    self.placeLabel.text = @"训练场地";
    self.placeLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.placeLabel];
    
    self.placeContentLabel = [[UILabel alloc] init];
    self.placeContentLabel.font = [UIFont systemFontOfSize:14];
    self.placeContentLabel.text = @"一乘驾校-场地训练B";
    self.placeContentLabel.textColor = [UIColor lightGrayColor];
    [containerView addSubview:self.placeContentLabel];
    
    self.coachLabel = [[UILabel alloc] init];
    self.coachLabel.font = [UIFont systemFontOfSize:17];
    self.coachLabel.text = @"指导教练";
    self.coachLabel.textColor = [UIColor yc_colorWithUInt:0x00b4ff];
    [containerView addSubview:self.coachLabel];
    
    self.coachContentLabel = [[UILabel alloc] init];
    self.coachContentLabel.font = [UIFont systemFontOfSize:14];
    self.coachContentLabel.text = @"阳教练";
    self.coachContentLabel.textColor = [UIColor lightGrayColor];
    [containerView addSubview:self.coachContentLabel];
    
    YCTrainMethodButton *comeButton = [[YCTrainMethodButton alloc] init];
    [comeButton setImage:[UIImage imageNamed:@"班车"] forState:UIControlStateNormal];//图片15*15
    [comeButton setTitle:@"前往班车" forState:UIControlStateNormal];
    [comeButton addTarget:self action:@selector(didTouchUpInsideComeButton:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:comeButton];
    self.comeButton = comeButton;
    
    YCTrainMethodButton *contactCoachButton = [[YCTrainMethodButton alloc] init];
    [contactCoachButton setImage:[UIImage yc_roundPlaceHolderImageForSize:CGSizeMake(15, 15)] forState:UIControlStateNormal];//图片15*15
    [contactCoachButton setTitle:@"联系教练" forState:UIControlStateNormal];
    [contactCoachButton addTarget:self action:@selector(didTouchUpInsideContactCoachButton:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:contactCoachButton];
    self.contactCoachButton = contactCoachButton;
    
    UIImageView *completeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"已完成戳"]];
    [containerView addSubview:completeImageView];
    self.completeImageView = completeImageView;
    
    [self layout];
}

- (void)layout
{
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
    }];
    //
    [self.daytLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView).offset(0);
        make.left.equalTo(self.containerView).offset(30);
    }];
    [self.monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.daytLabel.mas_bottom).offset(4);
        make.right.equalTo(self.daytLabel);
    }];
    //
    [self.projectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.monthLabel.mas_bottom).offset(heightFromIphone6Height(18));
        make.left.equalTo(self.daytLabel);
    }];
    [self.projectContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.projectLabel.mas_bottom).offset(heightFromIphone6Height(11));
        make.left.equalTo(self.daytLabel);
    }];
    //
    [self.comeTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.projectContentLabel.mas_bottom).offset(heightFromIphone6Height(46));
        make.left.equalTo(self.daytLabel);
    }];
    [self.comeTimeContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.comeTimeLabel.mas_bottom).offset(heightFromIphone6Height(11));
        make.left.equalTo(self.daytLabel);
    }];
    //
    [self.methodLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.comeTimeContentLabel.mas_bottom).offset(heightFromIphone6Height(46));
        make.left.equalTo(self.daytLabel);
    }];
    [self.methodContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.methodLabel.mas_bottom).offset(heightFromIphone6Height(11));
        make.left.equalTo(self.daytLabel);
    }];
    //
    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.methodContentLabel.mas_bottom).offset(heightFromIphone6Height(46));
        make.left.equalTo(self.daytLabel);
    }];
    [self.placeContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.placeLabel.mas_bottom).offset(heightFromIphone6Height(11));
        make.left.equalTo(self.daytLabel);
    }];
    
    //
    [self.coachLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.placeContentLabel.mas_bottom).offset(heightFromIphone6Height(46));
        make.left.equalTo(self.daytLabel);
    }];
    [self.coachContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.coachLabel.mas_bottom).offset(heightFromIphone6Height(11));
        make.left.equalTo(self.daytLabel);
    }];
    
    // 按钮布局
    [self.comeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.containerView.mas_right).offset(-30);
        make.top.equalTo(self.coachLabel).offset(15);
        make.width.equalTo(@130);
        make.height.equalTo(@35);
    }];
    [self.contactCoachButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.comeButton);
        make.top.equalTo(self.comeButton.mas_bottom).offset(10);
        make.width.equalTo(@130);
        make.height.equalTo(@35);
    }];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contactCoachButton.mas_bottom).offset(40);
    }];
    
    // 已完成
    [self.completeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contactCoachButton).offset(-33);
        make.centerY.equalTo(self.comeButton).offset(20);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 处理数据
- (void)loadData
{
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             @"pid":@(self.trainModel.pid),
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPITrainPlanDetail params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        NSDictionary *dict = response.data;
        self.model = [YCTrainDetailModel yc_objectWithKeyValues:dict];
        self.dataArray = self.model.content;
        [self reloadData];
        [self.scrollView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.scrollView.mj_header endRefreshing];
    }];

}


- (void)reloadData
{
    UIColor *unfinishColor = [UIColor yc_colorWithUInt:0x00b4ff];
    UIColor *finishColor = [UIColor darkGrayColor];
    NSInteger count = self.dataArray.count;
    for (NSInteger i = 0; i < count; i++) {
        if (i == 0) {
            self.projectLabel.text = self.dataArray[i][@"title"];
            self.projectContentLabel.text = self.dataArray[i][@"des"];
        } else if (i == 1)
        {
            self.comeTimeLabel.text = self.dataArray[i][@"title"];
            self.comeTimeContentLabel.text = self.dataArray[i][@"des"];
        }else if (i == 2)
        {
            self.methodLabel.text = self.dataArray[i][@"title"];
            self.methodContentLabel.text = self.dataArray[i][@"des"];
        }else if (i == 3)
        {
            self.placeLabel.text = self.dataArray[i][@"title"];
            self.placeContentLabel.text = self.dataArray[i][@"des"];
        }else if (i == 4)
        {
            self.coachLabel.text = self.dataArray[i][@"title"];
            self.coachContentLabel.text = self.dataArray[i][@"des"];

        }
    }
    self.daytLabel.text = [NSString stringWithFormat:@"%zd",self.model.timeday];
    self.monthLabel.text = self.model.timemonth;
    if (self.model.finishmark) {// 完成
        self.completeImageView.hidden = NO;//显示
        self.comeButton.hidden = YES;
        self.contactCoachButton.hidden = YES;
        self.daytLabel.textColor = finishColor;
        self.monthLabel.textColor = finishColor;
        self.projectLabel.textColor = finishColor;
        self.comeTimeLabel.textColor = finishColor;
        self.methodLabel.textColor = finishColor;
        self.placeLabel.textColor = finishColor;
        self.coachLabel.textColor = finishColor;
    } else {
        self.completeImageView.hidden = YES;
        self.comeButton.hidden = NO;
        self.contactCoachButton.hidden = NO;
        self.daytLabel.textColor = unfinishColor;
        self.monthLabel.textColor = unfinishColor;
        self.projectLabel.textColor = unfinishColor;
        self.comeTimeLabel.textColor = unfinishColor;
        self.methodLabel.textColor = unfinishColor;
        self.placeLabel.textColor = unfinishColor;
        self.coachLabel.textColor = unfinishColor;
    }
}
#pragma mark - 点击事件
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didTouchUpInsideComeButton:(YCTrainMethodButton *)button
{
    YCLogFunc
    YCCarServiceViewController *carVc = [[YCCarServiceViewController alloc] init];
    [self.navigationController pushViewController:carVc animated:YES];
}

- (void)didTouchUpInsideContactCoachButton:(YCTrainMethodButton *)button
{
    YCLogFunc
}

@end
